﻿using System;
using System.Web.Security;
using System.Web.UI.WebControls;
using BLL;

namespace ProductionAnalysis.Web
{
    public partial class ViewTest_ViewTest : System.Web.UI.Page
    {
        public string str1;
        public DateTime dt1
        {
            get { return DateTime.Parse(ViewState["dt1"].ToString()); }
            set { ViewState["dt1"] = value; }
        }
        public DateTime dt2
        {
            get { return DateTime.Parse(ViewState["dt2"].ToString()); }
            set { ViewState["dt2"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            str1 = Membership.GetUser().UserName.ToString();
            Label2.Text = str1;                     
        }

        protected void BindTest()
        {
            Test test = new Test();
            //ATEUser aTEUser = new ATEUser();
            //ATEUserInfo aTEUserInfo = aTEUser.GetATEUser(str1);
            //if (aTEUserInfo.UserID == str1)
            //{
                GridView1.DataSource = test.GetTestsbyUserID_DT(str1,dt1,dt2);
                GridView1.DataBind();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    if (GridView1.Rows[i].Cells[7].Text == "Fail")
                        GridView1.Rows[i].Cells[7].ForeColor = System.Drawing.Color.Red;
                }
            //}           
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["productSN"] = GridView1.SelectedDataKey["ProductSN"].ToString();
            Session["testTime"] = Convert.ToDateTime(GridView1.SelectedDataKey["TestTime"]);
            Session["ProductTypeID"] = GridView1.SelectedDataKey["ProductTypeID"].ToString();
            Session["UserID"] = GridView1.SelectedDataKey["UserID"].ToString();
            Session["ENAID"] = GridView1.SelectedDataKey["ENAID"].ToString();
            Session["SpecVersion"] = GridView1.SelectedDataKey["SpecVersion"].ToString();
            Session["IsPass"] = GridView1.SelectedDataKey["IsPass"].ToString();
            
            Response.Redirect("~/ViewTest/ViewYourself/ViewTestItem.aspx");
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindTest();
        }

        protected void BindPIMTest()
        {
            PIMTest test = new PIMTest();
            GridView2.DataSource = test.GetPIMTestbyUIDDT(str1, dt1, dt2);
            GridView2.DataBind();
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                if (GridView2.Rows[i].Cells[10].Text == "Fail")
                    GridView2.Rows[i].Cells[10].ForeColor = System.Drawing.Color.Red;
            }         
        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["productSN"] = GridView2.SelectedDataKey["ProductSN"].ToString();
            Session["testTime"] = Convert.ToDateTime(GridView2.SelectedDataKey["TestTime"]);

            Response.Redirect("~/ViewTest/ViewYourself/PIMTestItemViewbySNDT.aspx");
        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView2.PageIndex = e.NewPageIndex;
            BindPIMTest();
        }

        protected void BindUnion()
        {
            PackageProduct test = new PackageProduct();
            GridView3.DataSource = test.GetPackageTestsbyUIDDT(str1, dt1, dt2);
            GridView3.DataBind();
        }

        protected void GridView3_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView3.PageIndex = e.NewPageIndex;
            BindUnion();
        }

        protected void BindPackage()
        {
            PackageProduct test = new PackageProduct();
            GridView4.DataSource = test.GetPackagedProductsbyUIDDT(str1, dt1, dt2);
            GridView4.DataBind();         
        }

        protected void GridView4_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView4.PageIndex = e.NewPageIndex;
            BindPackage();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            dt1 = DateTime.Parse(this.TextBox1.Text + " 00:00:00");
            dt2 = DateTime.Parse(this.TextBox2.Text + " 00:00:00");
            BindTest();
            BindPIMTest();
            BindUnion();
            BindPackage();
        }
    }
}
